package com.avic.rong.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.mine.CapitalSubsidiaryActivity;
import com.avic.rong.bean.mine.MyMemberCardBean;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.media.tv.TvInputInfo.TYPE_COMPONENT;

public class MyMemberCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_STANDARD = 1001;
    private static final int TYPE_CHANGTO = 1002;
    private Context mContext;
    private ArrayList<MyMemberCardBean> mList;

    public MyMemberCardAdapter(Context mContext, ArrayList<MyMemberCardBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder viewHolder = null;
        if (i == TYPE_STANDARD){
            View standard = LayoutInflater.from(mContext).inflate(R.layout.item_standard_member, viewGroup,false);
            viewHolder = new StandardHolder(standard);
        }else if (i == TYPE_COMPONENT){
            View components = LayoutInflater.from(mContext).inflate(R.layout.item_components_member, viewGroup,false);
            viewHolder = new ComponentHolder(components);
        }else {
            View chang_to = LayoutInflater.from(mContext).inflate(R.layout.item_changto_card, viewGroup,false);
            viewHolder = new ChangtoHolder(chang_to);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        MyMemberCardBean bean = mList.get(i);
        int itemViewType = getItemViewType(i);
        if (itemViewType == TYPE_STANDARD){
            StandardHolder standardHolder = (StandardHolder) viewHolder;
            DecimalFormat df = new DecimalFormat("#####0.00");
            standardHolder.tv_balance.setText(" ¥ "+df.format(bean.getBalance()));
            standardHolder.tv_memeber_name.setText(bean.getTitle());
            standardHolder.tv_type_time.setText(bean.getTargetName()+" | 有效期至"+bean.getEndTime());
            standardHolder.btn_subsidiary.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = CapitalSubsidiaryActivity.queryIntent(mContext);
                    intent.putExtra("bzMemberCard",true);
                    intent.putExtra("balance",bean.getBalance());
                    mContext.startActivity(intent);
                }
            });
        }else if (itemViewType == TYPE_COMPONENT){
            ComponentHolder componentHolder = (ComponentHolder) viewHolder;
            componentHolder.tv_component_name.setText(bean.getTitle());
            DecimalFormat df = new DecimalFormat("#####0.00");
            componentHolder.tv_balance.setText(" ¥ "+df.format(bean.getBalance()));
            componentHolder.tv_endtime.setText(bean.getTargetName()+" | 有效期至"+bean.getEndTime());
            componentHolder.btn_subsidiary.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = CapitalSubsidiaryActivity.queryIntent(mContext);
                    intent.putExtra("memberCard",true);
                    intent.putExtra("balance",bean.getBalance());
                    mContext.startActivity(intent);
                }
            });
        }else {
            ChangtoHolder changtoHolder = (ChangtoHolder) viewHolder;
            changtoHolder.tv_changto_name.setText(bean.getTitle());
            changtoHolder.tv_endtime.setText("有效期至|"+bean.getEndTime());
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        int serviceType = mList.get(position).getServiceType();
        if (serviceType == 4){
            return TYPE_STANDARD;
        }else if (serviceType == 3){
            return TYPE_COMPONENT;
        }else {
            return TYPE_CHANGTO;
        }
    }

    public class ComponentHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_component_name)
        TextView tv_component_name;
        @BindView(R.id.tv_balance)
        TextView tv_balance;
        @BindView(R.id.btn_subsidiary)
        Button btn_subsidiary;
        @BindView(R.id.tv_endtime)
        TextView tv_endtime;

        public ComponentHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public class StandardHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_memeber_name)
        TextView tv_memeber_name;
        @BindView(R.id.tv_balance)
        TextView tv_balance;
        @BindView(R.id.btn_subsidiary)
        Button btn_subsidiary;
        @BindView(R.id.tv_type_time)
        TextView tv_type_time;
        public StandardHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public class ChangtoHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_changto_name)
        TextView tv_changto_name;

        @BindView(R.id.tv_endtime)
        TextView tv_endtime;

        public ChangtoHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
